<template>
	<view class="content">
		<image class="bg-img" src="../../static/index/img3.jpg"></image>
		<view class="form">
			<form :modelValue="updatefrom">
				<view class="inputWrapper">
					<input class="input" type="password" name="password" v-model="updatefrom.password"
						placeholder="请输入第一次密码" />
				</view>
				<view class="inputWrapper">
					<input class="input" type="password" name="passwordtwo" v-model="this.passwordtwo"
						placeholder="请输入第二次密码" />
				</view>
				<view>
					<button class="btnValue"  @click="submitForm()">保&nbsp;&nbsp;&nbsp;&nbsp;存</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				updatefrom: {
					username: '',
					password: '',
				},
				passwordtwo:'',
			}
		},
		onLoad() {
			this.updatefrom.username = wx.getStorageSync('studentnum');
		},
		methods: {
			submitForm(){
			if(this.updatefrom.username){
				let msg = '';
				if(this.updatefrom.password.length<6){
					msg = '密码长度小于6';
				}
				if(this.updatefrom.password!=this.passwordtwo){
					msg = '两次密码不一致';
				}
				if (!this.passwordtwo) {
					msg = '请输入第二次密码';
				}
				if (!this.updatefrom.password) {
					msg = '请输入第一次密码';
				}
				if(!msg){
					uni.request({
						url:`https://backendth.flymi.top/student/UpdateByStudentId/${this.updatefrom.username}`,
						method:"PUT",
						data:this.updatefrom,
						success:(res)=>{
							if(res.data.code == 200){
								uni.showToast({
									title: res.data.msg,
									icon: "success",
									duration: 2000
								})
								uni.removeStorageSync('token')
								uni.removeStorageSync('refreshToken')
								uni.removeStorageSync('studentnum')
								uni.removeStorageSync('nickName')
								uni.removeStorageSync('avatarUrl')
								uni.removeStorageSync('openid')
								uni.removeStorageSync('session_key')
								uni.reLaunch({
									url: '/pages/index/index'
								});
							} else if(res.data.code == 402 )
							{
								uni.showToast({
									title: res.data.msg,
									icon: "error",
									duration: 2000
								})
							}else{
								uni.showToast({
									title: '请联系管理员',
									icon: "error",
									duration: 2000
								})
							}
						}
					})
				}else{
					uni.showToast({
						title: msg,
						icon: "error",
						duration: 2000
					})
				}
			}else{
				uni.showToast({
					title: '未绑定学号',
					icon: "error",
					duration: 2000
				})
			}
		  }
		}
	}
</script>

<style>
	.text {
		margin-left: 34%;
		display: flex;
	}

	.name1 {
		margin-top: 15px;
		opacity: 0.85;
		border-radius: 40px;
		width: 100%;
		height: 30px;
		background-color: white;
		padding-top: 10px;
	}

	.bg-img {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.inputWrapper {
		width: 100%;
		height: 80upx;
		background: white;
		border-radius: 20px;
		box-sizing: border-box;
		padding: 0 20px;
		margin-top: 25px;
		opacity: 0.9;
	}

	.inputWrapper .input {
		width: 100%;
		height: 100%;
		text-align: center;
		font-size: 15px;
	}

	.btnValue {
		color: white;
		background-color: blueviolet;
		width: 100%;
		height: 80upx;
		background: #55aaff;
		border-radius: 50upx;
		margin-top: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: bold;
	}

	.form {
		padding: 50px;
		margin: 5px;
		margin-top: 50px;
		background-color: rgba(0, 0, 0, 0.1);
		border-radius: 20px;
	}

	.passwordtext {
		font-size: 18px;
		font-weight: 800;
		color: black;
		margin-left: 40%;
		margin-top: 10%;
	}
</style>
